package com.jz.community.moduleshopping.goodsDetail.model;

import android.content.Context;

import com.jz.community.basecomm.bean.baseGoods.BaseGoodsInfo;
import com.jz.community.basecomm.net.retrofit.ApiException;
import com.jz.community.basecomm.net.retrofit.ApiUtils;
import com.jz.community.basecomm.net.retrofit.HttpRxObservable;
import com.jz.community.basecomm.net.retrofit.RxLoadingWrapper;
import com.jz.community.basecomm.net.rxandroid.OnLoadListener;
import com.jz.community.moduleshopping.goodsDetail.bean.GoodsDetailInfo;
import com.jz.community.moduleshopping.goodsDetail.net.GoodsDetailApi;
import com.trello.rxlifecycle2.components.support.RxFragmentActivity;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;

/**
 * Created by Tony.Fan on 2018/3/27 09:54
 */
public class GoodsDetailModelImpl implements GoodsDetailModel {

    private Context mContext;

    public GoodsDetailModelImpl(Context context) {
        mContext = context;
    }

    @Override
    public void requestGoodsDetailInfo(String url, OnLoadListener<GoodsDetailInfo> onLoadListener) {
        Observable<GoodsDetailInfo> infoObservable = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, GoodsDetailApi.class)
                .getGoodsDetailInfo(url));
        new RxLoadingWrapper<GoodsDetailInfo>(mContext)
                .execute(infoObservable)
                .subscribe(new Consumer<GoodsDetailInfo>() {
                    @Override
                    public void accept(GoodsDetailInfo goodsDetailInfo) throws Exception {
                        if (goodsDetailInfo == null) {
                            return;
                        }
                        onLoadListener.onSuccess(goodsDetailInfo);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code);
                    }
                });
    }

    @Override
    public void requestDetailShopInfo(String page, String shopId, OnLoadListener<BaseGoodsInfo> onLoadListener) {
        Observable<BaseGoodsInfo> infoObservable = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, GoodsDetailApi.class)
                .getShopInfo("shopId::" + shopId, 0, 10));
        new RxLoadingWrapper<BaseGoodsInfo>(mContext,false)
                .execute(infoObservable)
                .subscribe(new Consumer<BaseGoodsInfo>() {
                    @Override
                    public void accept(BaseGoodsInfo shopGoodsInfo) throws Exception {
                        if (shopGoodsInfo == null) {
                            return;
                        }
                        onLoadListener.onSuccess(shopGoodsInfo);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code);
                    }
                });
    }

}
